//
//  BindingPhoneViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/11.
//

import UIKit

class BindingPhoneViewController: UIViewController {
    
    private var getCodeBtn = GSCaptchaButton()
    private var phoneTextF = UITextField()
    private var codeTextF = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "登录"
        
        addUI()
    }
    

    
    private func addUI() {
        self.view.addGradientColor(fromColors: [ColorWithStr("#FFD1D6").cgColor, ColorWithStr("#FFFFFF").cgColor, ColorWithStr("#F6F7F9").cgColor], locationArr: [0, 0.5, 1.0], with: .topToBottom)
        
        let title1L = UILabel().then({
            $0.frame = CGRect(x: 24, y: NAV_HEIGHT + 20, width: 200, height: 33)
            $0.text = "绑定手机号"
            $0.textColor = Color333333
            $0.font = SYSFONTBOLD(ofSize: 24)
        })
        view.addSubview(title1L)
        
        let title2L = UILabel().then({
            $0.frame = CGRect(x: 24, y: title1L.bottom + 10, width: 200, height: 33)
            $0.text = "绑定后可同时使用该手机登录"
            $0.textColor = Color333333
            $0.font = SYSFONT(ofSize: 14)
        })
        view.addSubview(title2L)
        
        let leftView = UIView()
        leftView.frame = CGRect(x: 0, y: 0, width: 20, height: 0)
        phoneTextF = UITextField().then {
            $0.frame = CGRect(x: 24, y: title2L.bottom + 20, width: SCREEN_WIDTH - 50, height: 56)
            $0.backgroundColor = ColorEEEEEE
            $0.font = SYSFONT(ofSize: 16)
            $0.placeholder = "请输入手机号码"
            $0.leftView = leftView
            $0.keyboardType = .numberPad
            $0.leftViewMode = .always
            $0.addRoundedCorners(withRadius: 28)
        }
        view.addSubview(phoneTextF)
        
        let left1View = UIView()
        left1View.frame = CGRect(x: 0, y: 0, width: 20, height: 0)
        codeTextF = UITextField().then {
            $0.frame = CGRect(x: 24, y: phoneTextF.bottom + 20, width: SCREEN_WIDTH - 50, height: 56)
            $0.backgroundColor = ColorEEEEEE
            $0.font = SYSFONT(ofSize: 16)
            $0.placeholder = "请输入验证码"
            $0.leftView = left1View
            $0.keyboardType = .numberPad
            $0.leftViewMode = .always
            $0.addRoundedCorners(withRadius: 20)
            $0.addBorder(withWidth: 1, borderColor: ColorEEEEEE)
        }
        view.addSubview(codeTextF)
        
        getCodeBtn = GSCaptchaButton().then {
            $0.frame = CGRect(x: codeTextF.width - 120, y: 0, width: 120, height: 56)
            $0.backgroundColor = MainColor
            $0.titleLabel?.font = SYSFONT(ofSize: 15)
            $0.setTitle("获取验证码", for: .normal)
            $0.setTitle("重新获取：second秒", for: .disabled)
            $0.setTitleColor(.white, for: .normal)
            $0.addRoundedCorners(withRadius: 28)
            $0.addTarget(self, action: #selector(getCodeBtnClick), for: .touchUpInside)
        }
        codeTextF.addSubview(getCodeBtn)
        
        let loginBtn = UIButton().then {
            $0.frame = CGRect(x: title1L.left, y: codeTextF.bottom + 140, width: codeTextF.width, height: 48)
            $0.backgroundColor = MainColor
            $0.titleLabel?.font = SYSFONT(ofSize: 14)
            $0.setTitle("确认", for: .normal)
            $0.setTitleColor(.white, for: .normal)
            $0.addRoundedCorners(withRadius: 24)
            $0.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        }
        view.addSubview(loginBtn)
        
        
    }
    
    @objc private func loginBtnClick() {
        let phoneStr = phoneTextF.text ?? ""
        let codeStr = codeTextF.text ?? ""
        guard phoneStr.count > 0 else {
            ShowHUD(str: "请输入手机号")
            return
        }
        guard codeStr.count > 0 else {
            ShowHUD(str: "请输入验证码")
            return
        }
        let param = [
            "captcha": codeStr,
            "mobile": phoneStr
        ]
        QINetworkManager.requestJson(urlStr: "/User/bindMobile", parameters: param) { json in
            Defaults[\.isLogin] = true
            
            let window = UIApplication.shared.keyWindow
            window?.switchRootCtrl()
        } fail: { json in
            let model = json.dictionaryObject?.kj.model(QIBaseModel.self) ?? QIBaseModel()
            if model.code == 103 {  //绑定手机号
                let bindingMobileCtl = BindingPhoneViewController()
                self.navigationController?.pushViewController(bindingMobileCtl, animated: true)
            } else if model.code == 105 {   //完善资料
                let ctl = ImproveInfoViewController()
                self.navigationController?.pushViewController(ctl, animated: true)
            } else if model.code == 106 || model.code == 107 {   //幸福感问卷调查
                let ctl = QuestionnaireSurveyViewController()
                ctl.code = model.code
                self.navigationController?.pushViewController(ctl, animated: true)
            }
        }
        
    }
    
    @objc private func getCodeBtnClick() {
        let phoneStr = phoneTextF.text ?? ""
        guard phoneStr.count > 0 else {
            ShowHUD(str: "请输入手机号")
            return
        }
        let param = [
            "event": "changemobile",
            "mobile": phoneStr
        ]
        QINetworkManager.requestJson(urlStr: "/Sms/send", parameters: param) { [self] json in
            getCodeBtn.maxSecond = 60
            getCodeBtn.countdown = true
        }
    }
    
    deinit {
        getCodeBtn.countdown = false
    }
}
